﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using HslCommunication;
using LongSCADA_WPF.Helpers;
using LongSCADA_WPF.Models;
using LongSCADA_WPF.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace LongSCADA_WPF.ViewModels
{
    public partial class DeviceViewModel:ObservableObject
    {
        [ObservableProperty]
        ScadaReadData _scadaReadDataProp = new();
        [ObservableProperty]
        string _logContent = string.Empty;

        private readonly GlobalHelper _globalHelper;
        private readonly UserModelService _userModelService;
        public DeviceViewModel(GlobalHelper globalHelper, UserModelService userModelService)
        {
            _globalHelper = globalHelper;
            _userModelService = userModelService;
            LogContent += $"程序运行中...{Environment.NewLine}";
        }

        [RelayCommand]
        void WriteDeviceControl(string paramName)
        {
            if (!_globalHelper.IsPLCConnected)
            {
                _userModelService.ShowMessage("PLC 未连接或者连接异常");
                LogContent += $"PLC 未连接或者连接异常{Environment.NewLine}";
                return;
            }
            string? address = _globalHelper.ReadEntityList.FirstOrDefault
                   (x => x.En == paramName)?.Address;
            if (string.IsNullOrEmpty(address))
            {
                _userModelService.ShowMessage($"未找到-{paramName}-对应的地址");
                LogContent += $"未找到-{paramName}-对应的地址{Environment.NewLine}";
                return;
            }
            OperateResult result = _globalHelper.Plc.Write(address, true);
            if (result.IsSuccess)
            {
               // _userModelService.ShowMessage($"写入-{paramName}-成功");
                LogContent += $"写入:{paramName}--地址:{address}--值:true{Environment.NewLine}";
            }
        }

        [RelayCommand]
        void ClearLog()
        {
            LogContent = string.Empty;
        }


        private bool CanToggleCollection()
        {
            LogContent += $"PLC 未连接或者连接异常{Environment.NewLine}";
            //ToggleCollectionCommand 的开关条件
            return _globalHelper.IsPLCConnected;
        }
        [RelayCommand(CanExecute = nameof(CanToggleCollection))]
        void ToggleCollection(string deviceName)
        {
            if (!_globalHelper.IsPLCConnected)
            {
                _userModelService.ShowMessage("PLC 未连接或者连接异常");
                LogContent += $"PLC 未连接或者连接异常{Environment.NewLine}";
                return;
            }
            ReadEntity? readAderss = _globalHelper.ReadEntityList.FirstOrDefault
                   (x => x.En == deviceName);
            if (readAderss == null)
            {
                _userModelService.ShowMessage($"未找到-{deviceName}-对应的地址");
                LogContent += $"未找到-{deviceName}-对应的地址{Environment.NewLine}";
                return;
            }

            //`
            bool value =(bool)_scadaReadDataProp.GetType().GetProperty(readAderss.En)?.GetValue(_scadaReadDataProp);

            OperateResult result = _globalHelper.Plc.Write(readAderss.Address, value);
            if (result.IsSuccess)
            {
                // _userModelService.ShowMessage($"写入-{paramName}-成功");
                LogContent += $"写入:{deviceName}--地址:{readAderss.Address}--值:true{Environment.NewLine}";
            }

        }

    }
}
